MODULE deposition

	USE definitions

    IMPLICIT NONE

CONTAINS

!------------------------------------------------------------
!Function to calculate deposition probability from wind on cell
!------------------------------------------------------------
FUNCTION calc_pd(pos, L)

	USE definitions
	USE functions

	IMPLICIT NONE

	INTEGER, DIMENSION(2), INTENT(in) :: pos
        REAL, INTENT(in) :: L
	REAL :: p1, p2,  calc_pd


           IF(ABS(L)<1/2) THEN
              p1 = 1
           ELSEIF(L<0) THEN
              p1 = 1
           ELSEIF(L>1/2) THEN
              p1 = 0
           ENDIF
        IF(atan(gradient_h(pos))<-15/180*pi .AND. dshadow == 1) THEN
           p1 = 1
       ENDIF


	IF (p1==1) THEN
		calc_pd = 1
		dshadowcount = dshadowcount + 1
	ELSEIF(p1==0 .AND. sandpit(pos(1),pos(2))%slabnumber > 0) THEN
		calc_pd = 0.03
	ELSEIF(p1/=1 .AND. sandpit(pos(1),pos(2))%slabnumber <= 0) THEN
		calc_pd = 0.05
		empty = empty + 1
	ENDIF 

END FUNCTION calc_pd
!------------------------------------------------------------


!------------------------------------------------------------
!Subroutine to depose slab on cell depending on deposition probaility
!------------------------------------------------------------
FUNCTION depose(pos, input, L)

  USE definitions
  USE functions
  
  IMPLICIT NONE
  
  REAL				:: r, pd
  INTEGER				:: depose
  INTEGER, INTENT(in), DIMENSION(2)	:: pos
  REAL, INTENT(in) :: input, L
  
  pd = calc_pd(pos, L)
  
  CALL random_number(r)
  IF(pd<r) THEN
     depose = 0
  ENDIF
  IF(pd>=r) THEN
     depose = 1
     Sandpit(pos(1),pos(2))%Slabnumber =  Sandpit(pos(1),pos(2))%Slabnumber + input
     IF(full == 1) THEN
        local_deposited_hight(pos(1),pos(2)) = local_deposited_hight(pos(1),pos(2)) + input
     ENDIF
  ENDIF
	
END FUNCTION depose
!------------------------------------------------------------

END MODULE deposition
